package com.sdgakj.utils;

import java.util.Date;
import org.dom4j.DocumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sdgakj.enums.EnumUserType;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;


/**
 * @ClassName: SMSSender
 * @Description: TODO 发送 短信
 * @author henryfang1@163.com
 * @date 2012-9-9 下午03:51:34
 * 
 */

public class SMSDYSender {
	private static Logger logger = LoggerFactory.getLogger(SMSDYSender.class);

	// 发送传递的参数
	private static String url="http://gw.api.taobao.com/router/rest";
	private static String AppKey ="23319617";
	private static String AppSecret="e64a5820f13caf3c1c2f81afe17d5abf";
	
	public static final String TEMPLATE_REG_USER = "SMS_7290080";
	public static final String TEMPLATE_CALLSERVICE_HANGUP = "SMS_18560036";
	public static final String TEMPLATE_CONFIRM_APPLY = "SMS_34530188";

    //代金券过期提醒
    public static final String TEMPLATE_COUPON_REMIND = "SMS_44395573";
	/**
	 * 发送短信
	 * 
	 * @author zhaozhe
	 * @time 2015年10月18日
	 * @param mobile
	 *            手机号
	 * @param content
	 *            内容
	 * @return
	 * @throws DocumentException 
	 */
	public static void sendSms(String mobile, String content){
		logger.info("手机号："+mobile+"调用大鱼api,验证码为："+content);
		String json="{\"number\":'"+content+"'}";  
		TaobaoClient client = new DefaultTaobaoClient(url, AppKey, AppSecret);  
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();  
		req.setExtend("123456");  
		req.setSmsType("normal");  
		req.setSmsFreeSignName("蜗牛二手货车");  
		req.setSmsParamString(json);  
		req.setRecNum(mobile);  
		req.setSmsTemplateCode("SMS_10680541");   
		try {  
		    AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req); 
			Object times = RedisClient
					.cacheGet(mobile.concat("_code_times_").concat(DateUtil.formatDateToString(new Date(), "yyyyMMdd")));
		    RedisClient.cacheAdd(
					mobile.concat("_code_times_").concat(DateUtil.formatDateToString(new Date(), "yyyyMMdd")),
					times.toString(), 60 * 60 * 24);
		    System.out.println(rsp.getBody());    
		    logger.info(rsp.getBody());
		} catch (Exception e) {  
		    // TODO: handle exception  
		}  
		
	}
	
	/**
	 * 短信发送 ； 无特殊参数，只是调用模板
	 * @author zhaozhe  xiaoyi edit
	 * @time 2015年10月18日
	 * @param servicetel   手机号
	 * @param templateCode 模板
	 * @return
	 * @throws ApiException 
	 * @throws DocumentException 
	 */
	public static void sendSmsWithNumber(String servicetel, String templateCode) throws ApiException {
		logger.info("客服电话：："+servicetel+"调用大鱼api");
		String json="{\"number\":"+EnumUserType.SERVICEHOTLINE.getName()+"}";  
		TaobaoClient client = new DefaultTaobaoClient(url, AppKey, AppSecret);  
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();  
		req.setExtend("123456");  
		req.setSmsType("normal");  
		req.setSmsFreeSignName("蜗牛二手货车");  
		req.setSmsParamString(json);  
		req.setRecNum(servicetel);  
		req.setSmsTemplateCode(templateCode);  
	    AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req); 
	    System.out.println(rsp.getBody());    
	    logger.info(rsp.getBody());
	}

	/**
	 * 用户申请车辆认证申请时发送短信
	 * @param mobile  车辆联系人
	 * @param content  模板需要的参数
	 */
	public static void sendConfirmSms(String mobile, String content){
		logger.info("手机号："+mobile+"调用认证申请短信api,输入参数："+content);
		String json="{\"fieldCombination\":'"+content+"',\"number\":'400-169-7799'}";
		TaobaoClient client = new DefaultTaobaoClient(url, AppKey, AppSecret);
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		req.setExtend("123456");
		req.setSmsType("normal");
		req.setSmsFreeSignName("蜗牛二手货车");
		req.setSmsParamString(json);
		req.setRecNum(mobile);
		req.setSmsTemplateCode(TEMPLATE_CONFIRM_APPLY);
		try {
			AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
			Object times = RedisClient
					.cacheGet(mobile.concat("_code_times_").concat(DateUtil.formatDateToString(new Date(), "yyyyMMdd")));
			RedisClient.cacheAdd(
					mobile.concat("_code_times_").concat(DateUtil.formatDateToString(new Date(), "yyyyMMdd")),
					times.toString(), 60 * 60 * 24);
			System.out.println(rsp.getBody());
			logger.info(rsp.getBody());
		} catch (Exception e) {
			// TODO: handle exception
		}

	}


    /**
     *
     * @param mobile  手机号
     * @param json   String json="{\"fieldCombination\":'"+content+"',\"number\":'400-169-7799'}";
     * @param template 模板编号
     */
    public static void defaultSend(String mobile, String json, String template){
        logger.info("手机号："+mobile+"调用认证申请短信api,输入参数："+ json);
       // String json="{\"fieldCombination\":'"+content+"',\"number\":'400-169-7799'}";
        TaobaoClient client = new DefaultTaobaoClient(url, AppKey, AppSecret);
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setExtend("123456");
        req.setSmsType("normal");
        req.setSmsFreeSignName("蜗牛二手货车");
        req.setSmsParamString(json);
        req.setRecNum(mobile);
        req.setSmsTemplateCode(template);
        try {
            AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
            System.out.println(rsp.getBody());
            logger.info(rsp.getBody());
        } catch (Exception e) {
            logger.error("",e);
        }

    }

	public static void main(String[] args) {
		sendSms("18611743529","6666");
	}
}